<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '菜单添加' : '菜单编辑'"
      :visible.sync="obj.isTrue"
      @close="cancel()"
      width="50%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="菜单名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select
            v-model="form.pid"
            placeholder="请选择上级菜单"
            @change="changePid()">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option v-for="item in menuList" :key="item.id" :label="item.title" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型">
          <el-radio :label="1" v-model="form.type" disabled>目录</el-radio>
          <el-radio :label="2" v-model="form.type" disabled>菜单</el-radio>
        </el-form-item>
        <el-form-item label="菜单图标" v-show="form.type == 1">
          <el-select v-model="form.icon" placeholder="请选择菜单图标">
            <el-option
              v-for="(item, index) in iconArr"
              :key="index"
              :value="item"
            >
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单地址" v-show="form.type == 2">
          <el-select v-model="form.url" placeholder="请选择菜单地址">
            <el-option
              v-for="(item,index) in secondRouter"
              :key="index"
              :label="item.meta.title"
              :value="'/' + item.path"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submit()" v-if="obj.isAdd"
          >添 加</el-button
        >
        <el-button type="primary" @click="edit()" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { secondRouter } from "../../../router/index.js";
import { menuAdd, menuInfo, menuEdit } from "../../../request/api.js";
import { successAlert, warningAlert } from "../../../util/alert.js";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      form: {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      //用来渲染菜单地址
      secondRouter: secondRouter,
      //用来渲染菜单图标
      iconArr: [
        "el-icon-user-solid",
        "el-icon-help",
        "el-icon-picture-outline-round",
        "el-icon-reading",
        "el-icon-house",
      ],
    };
  },
  computed: {
    ...mapGetters({
      menuList: "menu/list",
    }),
  },
  methods: {
    ...mapActions({
      reqMenuList: "menu/reqChangeList",
    }),
    //当上级菜单发生变化
    changePid() {
      if (this.form.pid == 0) {
        //让目录选中
        this.form.type = 1;
        //清空上一次选中的地址
        this.form.url = "";
      } else {
        //让菜单选中
        this.form.type = 2;
        //清空上一次选中的图标
        this.form.icon = "";
      }
    },
    //封装清空
    empty() {
      this.form = {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      };
    },
    //点击了取消
    cancel() {
      //关闭对话框
      this.obj.isTrue = false;
      //清空form数据
      this.empty();
    },
    //点击了添加
    submit() {
      menuAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          //成功的弹窗
          successAlert(res.data.msg);
          //关闭当前对话框并清空对话框数据
          this.cancel();
          //刷新列表数据
          this.reqMenuList();
        } else {
          //失败的弹窗
          warningAlert(res.data.msg);
        }
      });
    },
    //获取一条信息
    getOne(id) {
      menuInfo({ id: id }).then((res) => {
        //将数据赋值给form，form用来渲染页面
        this.form = res.data.list;
        //将id插入到form对象中，因为后面我们点击确定编辑的时候需要用到
        this.form.id = id;
      });
    },
    //点击了确定编辑
    edit() {
      menuEdit(this.form).then(res => {
        if (res.data.code == 200) {
          //成功的弹窗
          successAlert(res.data.msg);
          //关闭对话框并清空
          this.cancel();
          //重新请求列表数据
          this.reqMenuList();
        } else {
          //失败的弹窗
          warningAlert(res.data.msg);
        }
      });
    },
  },
  mounted() {},
};
</script>

<style scoped lang="">
</style>